﻿$(document).ready(function ()
{
    
    GetSubjectTypeList(); //获取已经设置好的考点
    ShowExamTime(); //获取已经设置好的考试时间
    $("#Sub_GetSubject").click(function ()
    {
        SetSubject(); //设置科目类型及考试时间
    });
    $("#btn_CompleteExam").click(function ()
    {
        CompleteExam(); //设置科目类型及考试时间
    });

});
function CompleteExam()
{
    //ajax验证数据
    $.ajax({ global: true, url: "PaperInstallRequest.aspx", data: { ActionName: "CompleteExam" },
        success: function (data)
        {
            if (data == true)
            {
                alert("上一批次考试结束,您可以进行新的考试科目设置！");
                $("#Sub_GetSubject").attr("disabled", false);
            }
        },
        error: function (e)
        {
            alert(e);
        },
        dataType: "json",
        type: "POST",
        async: true
    });
}

/*加载设置的考点*/
function GetSubjectTypeList()
{
    //ajax验证数据
    $.ajax({ global: true, url: "PaperInstallRequest.aspx", data: { ActionName: "GetSubjectType" },
        success: function (data)
        {
            if (data != null)
            {
                ShowSubjectTypeList(data);
            }
        },
        error: function (e)
        {
            alert(e);
        },
        dataType: "json",
        type: "POST",
        async: true
    });
}
/*勾选已经设置好的考点前面的复选框*/
function ShowSubjectTypeList(oLists)
{
    var chbs = $("#topic").find("input[name='More']");
    for (var i = 0; i < oLists.length; i++)
    {
        var SubjectType = oLists[i];
        for (var j = 0; j < chbs.length; j++)
        {
            if (SubjectType.SubjectTypeId == chbs[j].value)
            {
                $(chbs[j]).attr("checked", true);
            }
        }
    }
    if (oLists[0].CountPaperId > 0)
    {
        $("#Sub_GetSubject").attr("disabled", "disabled");
    }

}
function ShowExamTime()
{
    $.ajax({ global: true, url: "PaperInstallRequest.aspx", data: { ActionName: "ShowExamTime" },
        success: function (data)
        {
            if (data.ExamTime.length != 0)
            {
                GetExamTimeList(data);
            }
        },
        error: function (e)
        {
            alert(e);
        },
        dataType: "json",
        type: "POST",
        async: true
    });
}
function GetExamTimeList(oLists)
{
    var start = "";
    var end= "";
    var regS = new RegExp("\\/", "g");
    if (oLists.ExamTime[0].EndTime.indexOf('/') >= 0)
    {
        end = oLists.ExamTime[0].EndTime.replace(regS, "-");
        end = end.substring(0, end.length - 3);
        //处理考试结束时间
        var m1 = end.indexOf('-'); //从前往后获取第一个"-"符号的位置
        var m2 = end.lastIndexOf('-'); //从后往后获取第二个"-"符号的位置
        var m3 = end.indexOf(' '); //截取空格字符的位置 （2013-9-1 12:23）
        var m4 = end.indexOf(':'); //截取分号字符的位置
        var year2 = end.substring(0, m1);
        var month2 = end.substring(m1 + 1, m2); //月份
        var day2 = end.substring(m2 + 1, m3); //天数
        var hour2 = end.substring(m3 + 1, m4);//小时
        var minute2 = end.substring(m4 + 1);//分钟
        if (month2.length == 1)
        {
            month2 = "0" + month2;
        }
        if (day2.length == 1)
        {
            day2 = "0" + day2;
        }
        if (hour2.length == 1)
        {

            hour2 = "0" + hour2;
        }
        if (minute2.length == 1)
        {
            minute2 = "0" + minute2;
        }
        end = year2 + "-" + month2 + "-" + day2 +" "+ hour2 + ":" + minute2 ;

    }
    else
    {
        end = oLists.ExamTime[0].EndTime;
        end = end.substring(0, end.length - 3);
    }
    if (oLists.ExamTime[0].StartTime.indexOf('/') >= 0)
    {
        start = oLists.ExamTime[0].StartTime.replace(regS, "-");
        start = start.substring(0, start.length - 3);
        //处理考试开始时间
        var n1 = start.indexOf('-'); //从前往后获取第一个"-"符号的位置
        var n2 = start.lastIndexOf('-'); //从后往后获取第二个"-"符号的位置
        var n3 = start.indexOf(' '); //截取空格字符的位置 （2013-9-1 12:23）
        var n4 = start.indexOf(':'); //截取分号字符的位置
        var year1 = start.substring(0, n1);
        var month1 = start.substring(n1 + 1, n2); //月份
        var day1 = start.substring(n2 + 1, n3); //天数
        var hour1 = start.substring(n3 + 1, n4); //小时
        var minute1 = start.substring(n4 + 1);  //分钟
        if (month1.length == 1)
        {
            month1 = "0" + month1;
        }
        if (day1.length == 1)
        {
            day1 = "0" + day1;
        }
        if (hour1.length == 1)
        {
            hour1 = "0" + hour1;
        }
        if (minute1.length == 1)
        {
            minute1 = "0" + minute1;
        }
        start = year1 + "-" + month1 + "-" + day1 + " "+ hour1 + ":" + minute1;
    }
    else
    {
        start = oLists.ExamTime[0].StartTime;
        start = start.substring(0, start.length - 3);
    }
    document.getElementById("starttime").value = start;
    document.getElementById("endtime").value = end;
}
function SetSubject()
{
    
    var str = "";
    var startTime="";
    var endTime="";
    var n = 0;
    var s = document.getElementsByName('More');
    //设置单选、多选、判断题数量
    var once=document.getElementById('onceitem').value;//获取单选题个数的值
    var intonce = parseInt(once, 10); //转化为整数
    var more = document.getElementById('moreitem').value; //获取多选题个数的值
    var intmore = parseInt(more, 10); //转化为整数
    var deter= document.getElementById('deteritem').value; //获取判断题个数的值
    var intdeter = parseInt(deter, 10); //转化为整数
    if(once =="" || more == "" || deter == "") //判断输入数正确性
    {
       alert('题目个数不为空！');
       return false;
    }
    else
    {
        if (isNaN(intonce))
        {

            alert('单选题个数不是整数！');
            return false;
        }
        if (isNaN(intmore))
        {

            alert('多选题个数不是整数！');
            return false;
        }
        if (isNaN(intdeter))
        {

            alert('判断题个数不是整数！');
            return false;
        }
        if (intonce + intmore * 2 + intdeter != 100)
        {
            alert('总分加起来不是100分');
            return false;
        
        }

    }
    //设置考点
    for (var i = 0; i < s.length; i++)
    {
        if (s[i].checked == true)
        {
            str += s[i].value + ',';
            n++; //选中复选框个数
        }
    }
    str = str.substring(0, 2 * n - 1);
    if (n == 0)
    {
        alert("请设置考点！");
        return false;
    }
    else
    {
        if ($("#starttime").val() != "" && $("#endtime").val() != "")
        {
            startTime = $("#starttime").val();
            endTime = $("#endtime").val();
            $.ajax({ global: true, url: "PaperInstallRequest.aspx", data: { ActionName: "SetSubjectList", SubjectIdlist: str, StartTime: startTime, EndTime: endTime, OnceItem: intonce, MoreItem: intmore, DeterItem: intdeter },
                success: function (data)
                {
                    if (data == true)
                    {
                        alert('完成考试科目设置!');
                    }
                    else
                    {
                        alert('考试时间设置不合理');
                    }
                },
                error: function (e)
                {
                    alert(e);
                },
                dataType: "json",
                type: "POST",
                async: true
            });
        }
        else
        {
            alert('开始答题时间和结束答题时间不能为空');
            return false;
        }
    }
}
 